package com.yjz.beanValid;

import com.yjz.beanValid.annotation.EnumString;
import com.yjz.beanValid.annotation.ValidLicensePlate;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Valid;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;


import java.util.Set;

/**
 * @author: yuanjinzhong
 * @date: 2022/4/18 12:05 下午
 * @description:
 */
@Data
public class Person {
    @NotNull
    public String name;
    @NotNull
    @Min(0)
    public Integer age;

    @Valid
    @NotNull
    private Student student;

    /**
     * 校验字段只能有固定的枚举值
     */
    @EnumString(value = {"man","woman"})
    private String sex;

    /**
     * 校验身份证号码
     */
    @ValidLicensePlate
    private String cardNo;

    @Data
    public static class Student {
        @NotEmpty
        public String HomeAddress;
    }


    public static void main(String[] args) {
        Student student1 = new Student();
        student1.setHomeAddress("家庭地址");
        Person person = new Person();
        person.setAge(1);
        person.setName("张三");
        person.setSex("我是非法的性别");
        person.setCardNo("321281XY");
        person.setStudent(student1);
        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<Person>> result = validator.validate(person);
        result.stream().map(x -> x.getPropertyPath() + "" + x.getMessage() + ":" + x.getInvalidValue()).forEach(System.out::println);

        System.out.println(validator.getConstraintsForClass(person.getClass()).getConstrainedProperties());

    }
}
